﻿namespace Yom.Template
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    using Yom.Common;

    public static class MakeHtml
    {
        private static void SetCommon()
        {
            SkinTheme.ClearCache();
            SkinTheme.SetLabel("Common.SiteUrl", SiteLoad.Scheme.SiteUrl);
            SkinTheme.SetLabel("Common.Theme", SiteLoad.Scheme.Theme);
            SkinTheme.SetLabel("Common.SiteTitle", SiteLoad.Scheme.SiteTitle);
            SkinTheme.SetBlock("CategoryList", new Yom.Data.Category().GetCount());
            List<Yom.Model.Category> list = new Yom.Data.Category().GetList();
            int num = 0;
            foreach (Yom.Model.Category model in list)
            {
                SkinTheme.SetLabel(string.Format("CategoryList.SaveName_{0}", num), model.SaveName);
                SkinTheme.SetLabel(string.Format("CategoryList.Name_{0}", num), model.CategoryName);
                num++;
            }
            SkinTheme.SetLabel("Common.Ext", SiteLoad.Scheme.Ext);
            SkinTheme.SetLabel("Common.SaveFace", SiteLoad.Scheme.SaveFace);
            SkinTheme.SetLabel("Common.CopyRight", SiteLoad.Scheme.CopyRight.HtmlDeCode());
            SkinTheme.SetLabel("Common.BottomDesc", SiteLoad.Scheme.BottomDesc.HtmlDeCode());
        }

        private static void SetTopList()
        {
            List<Yom.Model.Comic> list = new Yom.Data.Comic().GetList("SELECT TOP 15 * FROM [Yom_Comic] WHERE [IsCheck]='True' ORDER BY [Visit] DESC");
            SkinTheme.SetBlock("TopList", list.Count);
            int num = 0;
            foreach (Yom.Model.Comic model in list)
            {
                int n = num + 1;
                SkinTheme.SetLabel(string.Format("TopList.Top_{0}", num), n.ToString());
                SkinTheme.SetLabel(string.Format("TopList.ID_{0}", num), n.ToString());
                SkinTheme.SetLabel(string.Format("TopList.SaveName_{0}", num), new Yom.Data.Category().GetModel(model.CategoryID).SaveName);
                SkinTheme.SetLabel(string.Format("TopList.TID_{0}", num), Math.Ceiling(Convert.ToDouble(model.ComicID) / 1000).ToString());
                SkinTheme.SetLabel(string.Format("TopList.CID_{0}", num), model.ComicID.ToString());
                SkinTheme.SetLabel(string.Format("TopList.Name_{0}", num), model.ComicName);
                SkinTheme.SetLabel(string.Format("TopList.Visit_{0}", num), model.Visit.ToString());
                num++;
            }
        }

        private static void SetRecommList()
        {
            List<Yom.Model.Comic> list = new Yom.Data.Comic().GetList("SELECT TOP 15 * FROM [Yom_Comic] WHERE [IsTop]='True' AND [IsCheck]='True' ORDER BY [Visit] DESC");
            SkinTheme.SetBlock("RecommList", list.Count);
            int num = 0;
            foreach (Yom.Model.Comic model in list)
            {
                SkinTheme.SetLabel(string.Format("RecommList.SaveName_{0}", num), new Yom.Data.Category().GetModel(model.CategoryID).SaveName);
                SkinTheme.SetLabel(string.Format("RecommList.CID_{0}", num), model.ComicID.ToString());
                SkinTheme.SetLabel(string.Format("RecommList.TID_{0}", num), Math.Ceiling(Convert.ToDouble(model.ComicID) / 1000).ToString());
                SkinTheme.SetLabel(string.Format("RecommList.FaceExt_{0}", num), model.FaceExt);
                SkinTheme.SetLabel(string.Format("RecommList.Name_{0}", num), model.ComicName);
                SkinTheme.SetLabel(string.Format("RecommList.LastName_{0}", num), new Yom.Data.Volume().GetLastVolume(model.ComicID).VolumeName);
                SkinTheme.SetLabel(string.Format("RecommList.State_{0}", num), new Yom.Data.Comic().GetState(model.State));
                num++;
            }
        }

        /// <summary>
        /// 验证传入日期是否为今天日期
        /// 
        /// 创建: 邱柯皓 2009/03/12 10:52
        /// </summary>
        /// <param name="dd1">要验证的日期</param>
        /// <returns>返回是否为今天日期</returns>
        private static bool CheckDate(DateTime dd1)
        {
            DateTime dd2 = DateTime.Now;
            TimeSpan s = dd2 - dd1;
            TimeSpan s2 = dd1 - dd2;

            if (s.Days == 0 && s2.Days == 0)
                return true;

            return false;
        }

        /// <summary>
        /// 获取图片路径加密字符串
        /// 
        /// 创建: 邱柯皓 2009/03/16 12:33
        /// </summary>
        /// <param name="ImgPath">图片路径</param>
        /// <returns>返回加密后的图片路径</returns>
        private static string GetImgEnCode(string ImgPath)
        {
            string prand = "";
            for (int i = 0; i < ImgPath.Length; i++)
            {
                char a = ImgPath[i];
                if (i != 0)
                    prand += "%";
                prand += ((int)a).ToString();
            }
            byte[] bytes = Encoding.UTF8.GetBytes(prand);
            return Convert.ToBase64String(bytes);
        }

        /// <summary>
        /// 生成首页
        /// 
        /// 创建: 邱柯皓 2009/03/12 10:24
        /// </summary>
        public static void MakeIndex()
        {
            SetCommon();
            SetTopList();
            SetRecommList();
            SkinTheme.SetLabel("Common.Keywords", SiteLoad.Scheme.KeyWords);
            SkinTheme.SetLabel("Common.Description", SiteLoad.Scheme.Description);
            SkinTheme.SetLabel("PageTitle", SiteLoad.Scheme.SiteTitle);
            List<Yom.Model.Note> noteList = new Yom.Data.Note().GetList(15, 1, "", "[IsTop]", false);
            SkinTheme.SetBlock("NoticeList", noteList.Count);
            int num = 0;
            foreach (Yom.Model.Note noteModel in noteList)
            {
                SkinTheme.SetElect(string.Format("NoticeList.HasUrl_{0}", num), !(string.IsNullOrEmpty(noteModel.Url)));
                SkinTheme.SetLabel(string.Format("NoticeList.Url_{0}", num), noteModel.Url);
                SkinTheme.SetLabel(string.Format("NoticeList.NoticeTitle_{0}", num), noteModel.Title);
                SkinTheme.SetLabel(string.Format("NoticeList.PostTime_{0}", num), noteModel.PostTime.ToString());
                SkinTheme.SetLabel(string.Format("NoticeList.ID_{0}", num), noteModel.NoteID.ToString());
                num++;
            }
            List<Yom.Model.Comic> comicFaceList = new Yom.Data.Comic().GetList("SELECT TOP 20 * FROM [Yom_Comic] WHERE [IsCheck]='True' ORDER BY [Visit] DESC");
            SkinTheme.SetBlock("FaceRecomm", comicFaceList.Count);
            num = 0;
            foreach (Yom.Model.Comic faceModel in comicFaceList)
            {
                SkinTheme.SetLabel(string.Format("FaceRecomm.SaveName_{0}", num), new Yom.Data.Category().GetModel(faceModel.CategoryID).SaveName);
                SkinTheme.SetLabel(string.Format("FaceRecomm.TID_{0}", num), Math.Ceiling(Convert.ToDouble(faceModel.ComicID) / 1000).ToString());
                SkinTheme.SetLabel(string.Format("FaceRecomm.CID_{0}", num), faceModel.ComicID.ToString());
                SkinTheme.SetLabel(string.Format("FaceRecomm.FaceExt_{0}", num), faceModel.FaceExt);
                SkinTheme.SetLabel(string.Format("FaceRecomm.Name_{0}", num), faceModel.ComicName);
                SkinTheme.SetLabel(string.Format("FaceRecomm.LastName_{0}", num), new Yom.Data.Volume().GetLastVolume(faceModel.ComicID).VolumeName);
                SkinTheme.SetLabel(string.Format("FaceRecomm.State_{0}", num), new Yom.Data.Comic().GetState(faceModel.State));
                num++;
            }
            List<Yom.Model.Comic> comicLastList = new Yom.Data.Comic().GetList("SELECT TOP 20 * FROM [Yom_Comic] WHERE [IsCheck]='True' ORDER BY [LastTime] DESC");
            SkinTheme.SetBlock("LastUpdateList", comicLastList.Count);
            num = 0;
            foreach (Yom.Model.Comic lastModel in comicLastList)
            {
                SkinTheme.SetLabel(string.Format("LastUpdateList.SaveName_{0}", num), new Yom.Data.Category().GetModel(lastModel.CategoryID).SaveName);
                SkinTheme.SetLabel(string.Format("LastUpdateList.TID_{0}", num), Math.Ceiling(Convert.ToDouble(lastModel.ComicID) / 1000).ToString());
                SkinTheme.SetLabel(string.Format("LastUpdateList.CID_{0}", num), lastModel.ComicID.ToString());
                SkinTheme.SetLabel(string.Format("LastUpdateList.Name_{0}", num), lastModel.ComicName);
                SkinTheme.SetLabel(string.Format("LastUpdateList.LastName_{0}", num), new Yom.Data.Volume().GetLastVolume(lastModel.ComicID).VolumeName);
                SkinTheme.SetElect(string.Format("LastUpdateList.Today_{0}", num), CheckDate(lastModel.LastTime));
                SkinTheme.SetLabel(string.Format("LastUpdateList.LastTime_{0}", num), lastModel.LastTime.ToString("yyyy-mm-dd"));
                num++;
            }
            List<Yom.Model.Link> linkList = new Yom.Data.Link().GetList();
            SkinTheme.SetBlock("FirendList", linkList.Count);
            num = 0;
            foreach (Yom.Model.Link linkModel in linkList)
            {
                SkinTheme.SetLabel(string.Format("FirendList.URL_{0}", num), linkModel.LinkUrl);
                SkinTheme.SetLabel(string.Format("FirendList.Name_{0}", num), linkModel.LinkName);
                num++;
            }
            string strContent = SkinTheme.RunParse("Index.html");
            string path = Path.Combine(Factory.RootPath, "Index." + SiteLoad.Scheme.Ext);
            Fso.WriteFile(path, strContent, true, Encoding.UTF8);
        }

        /// <summary>
        /// 生成分类页
        /// 
        /// 创建: 邱柯皓 2009/03/10 09:39
        /// </summary>
        /// <param name="CategoryID">分类编号</param>
        public static void MakeCategory(int CategoryID)
        {
            MakeCategory(CategoryID, false, false);
        }

        /// <summary>
        /// 生成分类页
        /// 
        /// 创建: 邱柯皓 2009/03/10 09:39
        /// </summary>
        /// <param name="CategoryID">分类编号</param>
        /// <param name="MakeC">是否生成漫画页</param>
        public static void MakeCategory(int CategoryID, bool MakeC)
        {
            MakeCategory(CategoryID, MakeC);
        }

        /// <summary>
        /// 生成分类列表
        /// 
        /// 创建: 邱柯皓 2009/03/09 20:57
        /// </summary>
        /// <param name="CategoryID">分类编号</param>
        /// <param name="MakeC">是否生成分类下漫画信息页</param>
        /// <param name="MakeV">是否生成漫画下分卷页, MakeC为true生效</param>
        public static void MakeCategory(int CategoryID, bool MakeC, bool MakeV)
        {
            Yom.Model.Category model = new Yom.Data.Category().GetModel(CategoryID);
            List<Yom.Model.Comic> list = new Yom.Data.Comic().GetList("SELECT * FROM [Yom_Comic] WHERE [IsCheck]='True' AND [CategoryID]=" + CategoryID);
            int pages = Math.Ceiling(Convert.ToDouble(list.Count) / 20).ToInt();
            int num = 0;
            for (int i = 1; i <= pages; i++)
            {
                SetCommon();
                SetRecommList();
                SetTopList();
                SkinTheme.SetLabel("PageTitle", SiteLoad.Scheme.SiteTitle + " -- " + model.CategoryName);
                SkinTheme.SetLabel("ListName", model.CategoryName);
                SkinTheme.SetLabel("Common.Keywords", model.SeoKey);
                SkinTheme.SetLabel("Common.Description", model.SeoDes);
                SkinTheme.SetLabel("List.SaveName", model.SaveName);
                List<Yom.Model.Comic> list2 = new Yom.Data.Comic().GetList(20, i, "[IsCheck]='True' AND [CategoryID]=" + CategoryID, false);
                SkinTheme.SetBlock("List", list2.Count);
                foreach (Yom.Model.Comic comic in list2)
                {
                    SkinTheme.SetLabel(string.Format("List.CID_{0}", num), comic.ComicID.ToString());
                    SkinTheme.SetLabel(string.Format("List.SaveName_{0}", num), model.SaveName);
                    SkinTheme.SetLabel(string.Format("List.TID_{0}", num), Math.Ceiling(Convert.ToDouble(comic.ComicID) / 1000).ToString());
                    SkinTheme.SetLabel(string.Format("List.FaceExt_{0}", num), comic.FaceExt);
                    SkinTheme.SetLabel(string.Format("List.Name_{0}", num), comic.ComicName);
                    SkinTheme.SetLabel(string.Format("List.Visit_{0}", num), comic.Visit.ToString());
                    SkinTheme.SetLabel(string.Format("List.LastName_{0}", num), (string.IsNullOrEmpty(new Yom.Data.Volume().GetLastVolume(comic.ComicID).VolumeName) ? "暂无更新" : new Yom.Data.Volume().GetLastVolume(comic.ComicID).VolumeName));
                    SkinTheme.SetLabel(string.Format("List.State_{0}", num), new Yom.Data.Comic().GetState(comic.State));
                    SkinTheme.SetLabel(string.Format("List.LastTime_{0}", num), comic.LastTime.ToString("yyyy-mm-dd"));
                    num++;
                }
                string strMulti = Factory.GetPager(20, i, list.Count, "List_<{Page}>." + SiteLoad.Scheme.Ext);
                SkinTheme.SetLabel("MultiPage", strMulti.Replace("List_1.", "Index."));
                string strContent = SkinTheme.RunParse("List.html");
                string dirPath = Path.Combine(Factory.RootPath, "Cartoon/" + model.SaveName);
                if (!Directory.Exists(dirPath))
                    Directory.CreateDirectory(dirPath);
                if (i == 1)
                {
                    Fso.WriteFile(Path.Combine(dirPath, "Index." + SiteLoad.Scheme.Ext), strContent, true, Encoding.UTF8);
                }
                else
                {
                    Fso.WriteFile(Path.Combine(dirPath, "List_" + i.ToString() + "." + SiteLoad.Scheme.Ext), strContent, true, Encoding.UTF8);
                }

                if (MakeC)
                {
                    foreach (Yom.Model.Comic comic2 in list2)
                    {
                        MakeCartoon(comic2.ComicID, MakeV);
                    }
                }
            }
        }

        /// <summary>
        /// 生成漫画信息页
        /// 
        /// 创建: 邱柯皓 2009/03/10 09:35
        /// </summary>
        /// <param name="ComicID">漫画编号</param>
        public static void MakeCartoon(long ComicID)
        {
            MakeCartoon(ComicID, false);
        }

        /// <summary>
        /// 生成漫画信息页
        /// 
        /// 创建: 邱柯皓 2009/03/09 22:42
        /// </summary>
        /// <param name="ComicID">漫画编号</param>
        /// <param name="MakeV">是否生成漫画下分卷</param>
        public static void MakeCartoon(long ComicID, bool MakeV)
        {
            Yom.Model.Comic model = new Yom.Data.Comic().GetModel(ComicID);
            SetCommon();
            SetRecommList();
            SetTopList();
            SkinTheme.SetLabel("Common.Keywords", SiteLoad.Scheme.KeyWords);
            SkinTheme.SetLabel("Common.Description", SiteLoad.Scheme.Description);
            SkinTheme.SetLabel("Comic.Name", model.ComicName);
            SkinTheme.SetLabel("Comic.LastName", new Yom.Data.Volume().GetLastVolume(model.ComicID).VolumeName);
            SkinTheme.SetLabel("Comic.State", new Yom.Data.Comic().GetState(model.State));
            SkinTheme.SetLabel("Comic.LastTime", model.LastTime.ToString());
            SkinTheme.SetLabel("PageTitle", SiteLoad.Scheme.SiteTitle + " -- " + new Yom.Data.Category().GetModel(model.CategoryID).CategoryName + " -- " + model.ComicName);
            SkinTheme.SetLabel("Comic.SaveName", new Yom.Data.Category().GetModel(model.CategoryID).SaveName);
            SkinTheme.SetLabel("Comic.TID", Math.Ceiling(Convert.ToDouble(model.ComicID) / 1000).ToString());
            SkinTheme.SetLabel("Comic.CID", model.ComicID.ToString());
            SkinTheme.SetLabel("Comic.FaceExt", model.FaceExt);
            SkinTheme.SetLabel("Comic.CategoryUrl", "Cartoon/" + new Yom.Data.Category().GetModel(model.CategoryID).SaveName);
            SkinTheme.SetLabel("Comic.CategoryName", new Yom.Data.Category().GetModel(model.CategoryID).CategoryName);
            SkinTheme.SetElect("Comic.OtherName", (!string.IsNullOrEmpty(model.OtherName)));
            SkinTheme.SetLabel("Comic.OtherName", model.OtherName);
            SkinTheme.SetLabel("Comic.Author", model.Author);
            SkinTheme.SetLabel("Comic.Language", new Yom.Data.Comic().GetLanguage(model.Language));
            SkinTheme.SetLabel("Comic.Color", new Yom.Data.Comic().GetColor(model.Color));
            SkinTheme.SetLabel("Comic.Tag", model.Tag);
            SkinTheme.SetLabel("Comic.Intro", model.Intro);
            List<Yom.Model.Volume> list = new Yom.Data.Volume().GetList(model.ComicID);
            SkinTheme.SetBlock("VolumeList", list.Count);
            int num = 0;
            foreach (Yom.Model.Volume volume in list)
            {
                SkinTheme.SetLabel(string.Format("VolumeList.ID_{0}", num), volume.VolumeID.ToString());
                SkinTheme.SetLabel(string.Format("VolumeList.Name_{0}", num), volume.VolumeName);
                num++;
            }
            string strContent = SkinTheme.RunParse("ComicInfo.html");
            string dirPath = Path.Combine(Factory.RootPath, "Cartoon/" + new Yom.Data.Category().GetModel(model.CategoryID).SaveName) + "/" + Math.Ceiling(Convert.ToDouble(model.ComicID) / 1000).ToString() + "/" + model.ComicID.ToString();
            if (!Directory.Exists(dirPath))
                Directory.CreateDirectory(dirPath);
            Fso.WriteFile(Path.Combine(dirPath, "Index." + SiteLoad.Scheme.Ext), strContent, true, Encoding.UTF8);

            if (MakeV)
            {
                foreach (Yom.Model.Volume volume in list)
                {
                    MakeVolume(volume.VolumeID);
                }
            }
        }

        /// <summary>
        /// 生成分卷
        /// 
        /// 创建: 邱柯皓 2009/03/09 23:03
        /// </summary>
        /// <param name="VolumeID">分卷编号</param>
        public static void MakeVolume(long VolumeID)
        {
            Yom.Model.Volume model = new Yom.Data.Volume().GetModel(VolumeID);
            Yom.Model.Comic comic = new Yom.Data.Comic().GetModel(model.ComicID);
            List<Yom.Model.Chapter> list = new Yom.Data.Chapter().GetList(VolumeID);
            int num = 0;
            SkinTheme.SetLabel("Common.Keywords", SiteLoad.Scheme.KeyWords);
            SkinTheme.SetLabel("Common.Description", SiteLoad.Scheme.Description);
            SkinTheme.SetLabel("PageTitle", SiteLoad.Scheme.SiteTitle + " -- " + comic.ComicName + " -- " + model.VolumeName);
            SkinTheme.SetLabel("Common.SiteUrl", SiteLoad.Scheme.SiteUrl);
            SkinTheme.SetLabel("Common.Theme", SiteLoad.Scheme.Theme);
            SkinTheme.SetLabel("Common.SiteTitle", SiteLoad.Scheme.SiteTitle);
            SkinTheme.SetLabel("Comic.SaveName", new Yom.Data.Category().GetModel(comic.CategoryID).SaveName);
            SkinTheme.SetLabel("Comic.TID", Math.Ceiling(Convert.ToDouble(comic.ComicID) / 1000).ToString());
            SkinTheme.SetLabel("Comic.CID", comic.ComicID.ToString());
            SkinTheme.SetLabel("Common.Ext", SiteLoad.Scheme.Ext);
            SkinTheme.SetLabel("Comic.Name", comic.ComicName);
            SkinTheme.SetLabel("Volume.Name", model.VolumeName);
            SkinTheme.SetLabel("AllPage", list.Count.ToString());
            SkinTheme.SetBlock("PageList", list.Count);
            string ChapterList = string.Empty;
            for (int i = 1; i <= list.Count; i++)
            {
                ChapterList += "chls[" + i + "] = '" + GetImgEnCode(list[i - 1].ImgPath) + "';";
            }
            SkinTheme.SetLabel("Chapters.List", ChapterList);
            num = 0;
            for (int i = 1; i <= list.Count; i++)
            {
                SkinTheme.SetLabel(string.Format("PageList.ID_{0}", num), i.ToString());
                num++;
            }
            SkinTheme.SetElect("Volume.PreVolume", new Yom.Data.Volume().GetPreVolume(model.VolumeID).VolumeID > 0);
            SkinTheme.SetLabel("Volume.PreID", new Yom.Data.Volume().GetPreVolume(model.VolumeID).VolumeID.ToString());
            SkinTheme.SetElect("Volume.NextVolume", new Yom.Data.Volume().GetNextVolume(model.VolumeID).VolumeID > 0);
            SkinTheme.SetLabel("Volume.NextID", new Yom.Data.Volume().GetNextVolume(model.VolumeID).VolumeID.ToString());
            string strContent = SkinTheme.RunParse("Chapter.html");
            string path = Path.Combine(Factory.RootPath, "Cartoon/" + new Yom.Data.Category().GetModel(comic.CategoryID).SaveName + "/" + Math.Ceiling(Convert.ToDouble(comic.ComicID) / 1000).ToString() + "/" + comic.ComicID.ToString() + "/");
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);
            Fso.WriteFile(Path.Combine(path, model.VolumeID.ToString() + "." + SiteLoad.Scheme.Ext), strContent, true, Encoding.UTF8);
        }
    }
}